﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using LuxuryResort.WebSite.LRMPDataService;

namespace LuxuryResort.WebSite.LocalAdmin
{
    public partial class LocalReport : Page
    {
        private readonly RoomServiceClient _svc = new RoomServiceClient();

        private Guid UserID
        {
            get
            {
                if (ViewState["UserID"] == null && HttpContext.Current.User != null)
                {
                    string userName = HttpContext.Current.User.Identity.Name;
                    MembershipUser membershipUser = Membership.GetUser(userName);
                    if (membershipUser != null)
                    {
                        object userId = membershipUser.ProviderUserKey;
                        ViewState["UserID"] = userId;
                    }
                }
                return (Guid) ViewState["UserID"];
            }
        }

        private Guid ResortId
        {
            get { return new Guid(ConfigurationManager.AppSettings["ResortId"]); }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!IsPostBack)
            //{
            //    BindData();
            //}
        }

        protected void DataPagerRooms_PreRender(object sender, EventArgs e)
        {
            try
            {
                List<LRMPReport> data =
                    _svc.GetReport(UserID, ResortId).Where(d => d.Category == "Room").OrderBy(x => x.RoomNo).ToList();
                lvRoomSumary.DataSource = data;
                lvRoomSumary.DataBind();
            }
            catch (Exception ex)
            {
                Response.Redirect("~/AccessDenied.aspx");
            }
        }

        protected void DataPagerOrders_PreRender(object sender, EventArgs e)
        {
            try
            {
                List<LRMPReport> data =
                    _svc.GetReport(UserID, ResortId).Where(d => d.Category != "Room").OrderBy(x => x.FBName).ToList();
                lvFoodSumary.DataSource = data;
                lvFoodSumary.DataBind();
            }
            catch (Exception ex)
            {
                Response.Redirect("~/AccessDenied.aspx");
            }
        }

        private void BindData()
        {
            LRMPReport[] data = _svc.GetReport(UserID, ResortId);

            BindRoomData(data.Where(d => d.Category == "Room").OrderBy(x => x.RoomNo));
            BindFoodData(data.Where(d => d.Category != "Room").OrderBy(x => x.FBName));
        }

        private void BindRoomData(IEnumerable<LRMPReport> data)
        {
            lvRoomSumary.DataSource = data;
            lvRoomSumary.DataBind();
        }

        private void BindFoodData(IEnumerable<LRMPReport> data)
        {
            lvFoodSumary.DataSource = data;
            lvFoodSumary.DataBind();
        }
    }
}